package com.ysh.paping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysh.bean.Student;
import com.ysh.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/*
    分页插件练习
       分页插件依赖于MyBatis
 */
public class Test01 {
    private SqlSession sqlSession;
    private StudentMapper mapper;
    private InputStream is;

    @Before
    public void before() throws Exception {
        is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        sqlSession = sqlSessionFactory.openSession();

        mapper = sqlSession.getMapper(StudentMapper.class);

    }

    @After
    public void After() throws Exception {
        sqlSession.commit();
        sqlSession.close();
        is.close();
    }

    @Test
    public void findAll() {
        // 通过分页助手，设置分页参数
        // 当前页索引：（当前页-1）*当前页码
        // 第一页
        PageHelper.startPage(1, 2);//当前页，每页条数，
        List<Student> list = mapper.selectAll();
        for (Student student : list) {
            System.out.println(student);
        }

        // 获取分页相关参数,插件包含一个PageBean对象
        PageInfo<Student> info = new PageInfo<>(list);
        System.out.println("总条数：" + info.getTotal());
        System.out.println("总页数：" + info.getPages());
        System.out.println("当前页：" + info.getPageNum());
        System.out.println("每页显示条数：" + info.getPageSize());
        System.out.println("上一页：" + info.getPrePage());
        System.out.println("下一页：" + info.getNextPage());
        System.out.println("是否是第一页：" + info.isIsFirstPage());
        System.out.println("是否是最后一页：" + info.isIsLastPage());

    }


}
